<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Selectuser extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');
	}
	function selectuser_list(){
		$this->load->view('selectuser/user');	
	}
	function getcheckeduser(){
		$iddata = array();
		$namedata = array();
		$deptid = $this->uri->segment(3, 0);
		$query = $this->db->where('parent_id',$deptid)->get('dept');
		$checkedall = $this->getAllIds($query);//将取得的多维数组转一维数组
		$checkedall = ($checkedall != '')?$checkedall:$deptid;
		$query = $this->db->where_in('dept_id',array_filter(explode(',',$checkedall)))->get('users');
		foreach ($query->result() as $v){
			$iddata[] = $v->user_id;
			$namedata[] = $v->user_name;
		}
		$listdata = array("id"=>$iddata,'name'=>$namedata);
		echo json_encode($listdata); 

	}
	function getAllIds($query,$arr=''){  
		foreach ($query->result_array() as $row){
			$query = $this->db->where('parent_id',$row['dept_id'])->get('dept');
			if(count($query->result_array())>0){
		   		@$arr .= $this->getAllIds($query,$arr); 
			}
		   	@$arr .= $row['dept_id'].',';
		}
		return @$arr;
	}
}
?>